vim.g.mapleader = ','

-- Check https://github.com/neovim/neovim/issues/9888
vim.opt.fsync = true

vim.opt.ignorecase = true

vim.opt.termguicolors = true

vim.opt.number = true
vim.opt.relativenumber = true

vim.opt.signcolumn = 'yes'
vim.opt.cursorline = true

vim.opt.tabstop = 4
vim.opt.shiftwidth = 4
-- Yes, no tabs! `set noexpandtab` and `%retab!` if someone is mad at this
vim.opt.expandtab = true

vim.opt.scrolloff = 5

vim.opt.list = true
vim.opt.listchars = {
    tab = '›·', -- » ❭ › ⇥ ↦ ·⬝
    trail = '·'
}

vim.opt.fillchars = {
    -- Do not show EndOfBuffer symbol
    eob = ' '
}

-- vim.opt.linebreak = true -- break on word boundies
vim.opt.showbreak = '↳'

vim.opt.timeoutlen = 2000

vim.go.shada = "'2000"

-- this is actually the default:
-- vim.cmd('filetype plugin indent on')

local dicts_dir = ScriptRoot .. 'base/dicts/'
vim.opt.dict = require('lin.utils').listdir_uv(dicts_dir, function(name, typ)
    return typ == 'file' and dicts_dir .. name or nil
end)

function Tabline()
    local fmt_str = ''
    local cur_tab = vim.api.nvim_get_current_tabpage()
    for i, tab in ipairs(vim.api.nvim_list_tabpages()) do
        local cur_buf = vim.api.nvim_win_get_buf(vim.api.nvim_tabpage_get_win(tab))
        local name = vim.fs.basename(vim.api.nvim_buf_get_name(cur_buf))
        if name == '' then
            name = '[Unnamed]'
        end
        local is_cur_tab = cur_tab == tab
        fmt_str = fmt_str .. '%' .. tostring(i) .. 'T'
        fmt_str = fmt_str .. (is_cur_tab and '%#TabLineSel#⟩ ' or '%#TabLine#┊ ')
        fmt_str = fmt_str .. tostring(i) .. ' ' .. name .. ' '
        --[[ local win_count = vim.fn.tabpagewinnr(i, '$')
        if win_count > 1 then
            fmt_str = fmt_str .. '(+' .. tostring(win_count - 1) .. 'W) '
        end ]]
    end

    fmt_str = fmt_str .. '%#TabLineFill#%T'
    return fmt_str
end
vim.go.tabline = '%!v:lua.Tabline()'
